package com.cy.pj.common.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/*切面的优先级需要借助@Order注解进行描述，数字越小优先级越高，默认优先级比较低。*/
@Order(1)
@Aspect
@Component
public class CacheAspect {//系统底层会将这个切面中的内容转换成Advisor对象

    //假如这个map就是一个cache，存储来自数据库的数据
    private Map<String,Object> cache=new ConcurrentHashMap<>();

    //@Pointcut("bean(moduleServiceImpl)")
   @Pointcut("@annotation(com.cy.pj.common.annotation.RequiredCache)")
    public void doCache(){}

    @Around("doCache()")
    public Object around(ProceedingJoinPoint joinPoint)throws  Throwable{
        Object obj=cache.get("userPer");
        if(obj!=null)return obj;
        obj=joinPoint.proceed();
        cache.put("userPer",obj);
        return obj;

    }
}
